package com.sellerSystem.controller;

import java.net.InetAddress;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

import javax.jms.Connection;
import javax.jms.ConnectionFactory;
import javax.jms.Destination;
import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.MessageConsumer;
import javax.jms.MessageListener;
import javax.jms.MessageProducer;
import javax.jms.Session;
import javax.jms.TextMessage;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.apache.activemq.ActiveMQConnectionFactory;
import org.apache.activemq.command.ActiveMQQueue;
import org.json.JSONException;
import org.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.sellerSystem.service.SellerManager;
import com.sellerSystem.vo.Commodity;
import com.sellerSystem.vo.Seller;
import com.sellerSystem.vo.SellerAddress;

@Controller
public class SellerManagerController {
	@Autowired(required = true)
	private SellerManager sm;

	@RequestMapping("login")
	public String login(String sellerTel, String sellerPsd,
			HttpServletRequest request) {
		Seller seller = sm.login(sellerTel, sellerPsd);
		if (seller != null) {
			request.getSession().setAttribute("seller", seller);
			int s = seller.getSellerState();
			if (s == 0) {
				return "approvaling";
			} else if (s == 2) {
				return "unapproval";
			} else if (s == 3) {
				return "blackbill";
			} else
				return "loginSuc";

		} else {
			return "fail";
		}

	}

	@RequestMapping("regist")
	public String regist(Seller seller, SellerAddress sa, String sellerTel,
			HttpServletRequest request) {
		seller.setSellerAddress(sa);
		if (sm.isExist(sellerTel) == false) {
			int rs = sm.regist(seller);
			// request.getSession().setAttribute("", arg1);
			if (rs != 0)
				return "registSuc";
			else
				return "regist";
		} else {
			return "registFail";
		}
	}

	@RequestMapping("findseller")
	@ResponseBody
	public ArrayList<Seller> findSeller(HttpServletRequest request)
			throws Exception {
		

		ArrayList<Seller> ls = sm.seller();
		return ls;
	}

	@RequestMapping("loginSuc")
	@ResponseBody
	public ArrayList<Commodity> shopping(HttpServletRequest request) {

		HttpSession session = request.getSession();
		Seller seller_session = (Seller) session.getAttribute("seller");
		String sellerName = seller_session.getSellerName();

		if (sellerName != null) {
			ArrayList<Commodity> lc = sm.findCommodity(sellerName);
			request.getSession().setAttribute("lc", lc);
			// List<Commodity> commoditylist = new ArrayList<Commodity>();
			// commoditylist.add(commodity);
			return lc;
		}
		return null;

	}

	@RequestMapping("addCommodity")
	public String addCommodity(Commodity commodity, HttpServletRequest request) {
		sm.addCommodity(commodity);
		return "addSuc";
	}

	@RequestMapping("findsellerbyid")
	@ResponseBody
	public Seller finSellerById(Integer sellerId) {

		Seller s = sm.findId(sellerId);
		System.out.println(s.toString());
		return s;
	}
	@RequestMapping("modifycommodity")
	public void modifyCommodity(Commodity commodity,HttpServletRequest request){
		System.out.println(commodity.getCommodityName());
		System.out.println(commodity.toString());
		sm.modifyCommodity(commodity);
	}
	@RequestMapping("deletecommodity")
	public void deleteCommodity(Commodity commodity,HttpServletRequest request){
		System.out.println("@@@"+commodity);
		sm.deleteCommodity(commodity);
	}
}
